package com.zl.pet.service.impl;

import com.zl.basic.service.impl.BaseServiceImpl;
import com.zl.pet.domain.PetType;
import com.zl.pet.mapper.PetTypeMapper;
import com.zl.pet.service.IPetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    @Override
    public List<PetType> petTree() {

        List<PetType> all = petTypeMapper.loadAll();
        Map<Long, PetType> map  = new HashMap<>();
        for (PetType petType : all) {
            map.put(petType.getId(),petType);
        }

        List<PetType> PetTree = new ArrayList<>();
        for (PetType petType:all){
            if(petType.getPid() == null){
                PetTree.add(petType);
            }else{
                Long parent_id = petType.getPid();
                PetType perentpettype = map.get(parent_id);
                perentpettype.getChildren().add(petType);
            }
        }
        return PetTree;
    }
}
